package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.Activities;
import com.hebeu.gfh.pojo.po.ActivityApplication;
import com.hebeu.gfh.pojo.po.ClubApplication;
import com.hebeu.gfh.pojo.vo.*;
import com.hebeu.gfh.service.activitiesService;
import com.hebeu.gfh.service.activityApplicationService;
import com.hebeu.gfh.service.clubsService;
import com.hebeu.gfh.service.userService;
import com.hebeu.gfh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/activityApplication")
public class activityApplicationController {
    @Autowired
    activityApplicationService applicationService;
    @Autowired
    userService userService;
    @Autowired
    activitiesService activitiesService;
    @Autowired
    clubsService clubsService;
    //提交申请
    @PostMapping("/submitApplication")
    public R submitApplication(ActivityApplication application){
        try {
            applicationService.submitApplication(application);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("----------------------------");
            return R.error(500,"提交申请失败");
        }
        return R.ok("提交申请成功");
    }
    //根据userid获取已提交的申请
    @PostMapping("/getApplicationByuserId")
    public R getApplicationByuserId(int userId){
        List<ActivityApplication> applications =applicationService.getApplicationByuserId(userId);
        return R.ok(applications);
    }

    //根据userId和activityId查询是否已经提交过了
    //根据userid获取已提交的申请
    @PostMapping("/isApplicated")
    public R isApplicated(int userId,int activityId){
       int sum = applicationService.isApplicated(userId,activityId);
        return R.ok(sum);
    }
    //进行审批
    @PostMapping("/approveApplication")
    public R approveApplication(int applicationId,int applicationState){
        try {
            applicationService.approveApplication(applicationId,applicationState);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("---------------------------------");
            return R.error(500,"审批失败");
        }
        return  R.ok("审批成功");
    }

    //根据审批状态查询请求
    @PostMapping("/getApplicationByState")
    public R getApplicationByState(){
        return R.ok();
    }

    @PostMapping("/getApplicationByUserId")
    public R getApplicationByUserId(IdAndName idAndName){
        List<ActivityApplication> clubApplicationList =applicationService.getApplicationByUserId(idAndName);
        List<activityAndInfo> andInfos = new ArrayList<>();
        for(ActivityApplication application:clubApplicationList){
            appinfo u = userService.findUserByuserId(application.getUserId());
            String username = u.getUserInfo().getUsername();
            Activities activities = activitiesService.findActivitiesByactivityId(application.getActivityId());
            String activityName = activities.getActivityName();
            activityAndInfo  a = new activityAndInfo();
            a.setApplication(application);
            a.setUsername(username);
            a.setActivityName(activityName);
            andInfos.add(a);
        }
        return R.ok(andInfos);
    }

    //通过
    @PostMapping("/passApplication")
    public R passApplication(int applicationId){
        try {
            applicationService.passApplication(applicationId);
        }catch (Exception e){
            return R.error(500,"操作失败");
        }
        return R.ok("操作成功");
    }

    //驳回
    @PostMapping("/unpassApplication")
    public R unpassApplication(int applicationId){
        try {
            applicationService.unpassApplication(applicationId);
        }catch (Exception e){
            return R.error(500,"操作失败");
        }
        return R.ok("操作成功");
    }

    //根据userId查询已经申请参加的活动
    @PostMapping("/getActivityApplicationByUserId")
    public R getActivityApplicationByUserId(int userId){
    List<activityApplicationAndClub> applicationAndClubList = new ArrayList<>();
    List<ActivityApplication> applications = applicationService.getActivityApplicationByUserId(userId);
    for(ActivityApplication application:applications){
        Activities activities = activitiesService.findActivitiesByactivityId(application.getActivityId());
        activityApplicationAndClub applicationAndClub = new activityApplicationAndClub();
        String clubName = clubsService.getClubNameById(activities.getClubId());
            applicationAndClub.setApplicationId(application.getApplicationId());
            applicationAndClub.setApplicationContent(application.getApplicationContent());
            applicationAndClub.setApplicationTime(application.getApplicationTime());
            applicationAndClub.setApplicationState(application.getApplicationState());
            applicationAndClub.setClubName(clubName);
            applicationAndClub.setActivityId(application.getActivityId());
            applicationAndClub.setActivityName(activities.getActivityName());
        applicationAndClubList.add(applicationAndClub);
    }
    return R.ok(applicationAndClubList);
    }
}
